package cn.gok.service.Impl;

import cn.gok.mapper.CheckMapper;
import cn.gok.pojo.Log;
import cn.gok.pojo.Practice;
import cn.gok.service.CheckService;
import cn.gok.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CheckServiceImpl implements CheckService {
    @Autowired
    private CheckMapper checkMapper;

    private Tool tool=new Tool();

    @Override
    public String judge(List<Practice> answer, Map questions) {
//        计算得分
        int num=0;
        String msg="";
        for (int i = 0; i <answer.size() ; i++) {
//            判断学生是否有答这道题目
            if(questions.get(answer.get(i).getSerial())!=null){
//                这里对题目进行校验
                if(!tool.validation(checkMapper.sel(answer.get(i).getAnswer()),checkMapper.sel((String) questions.get(answer.get(i).getSerial())))){
                    msg=msg+" 第"+answer.get(i).getSerial()+"题";
                }else{
//                    正确加分
                    num+=Integer.getInteger(answer.get(i).getScore());
                }
            }else{
//                这里处理没有提交的题目
                msg=msg+" 第"+answer.get(i).getSerial()+"题";
            }
        }
        if(msg!=""){
            msg="做错的题目有："+msg;
        }else{
            msg="恭喜你，全部正确！";
        }
        msg+=" 得分为："+num;
        return msg;
    }

    @Override
    public int judge_log(String username, String content, String result, String title) {
        return checkMapper.judge_log(username,content,result,title);
    }

    @Override
    public int del(int id) {
        return checkMapper.del(id);
    }

    @Override
    public List<Log> selof(String query) {
        return checkMapper.selof(query);
    }
}
